import { Injectable } from '@angular/core';
import {
    ConfigAndPermisions,
    ContentConfig,
    ContentServiceProxy,
    FormField
} from '@shared/service-proxies/service-proxies';

import { finalize } from 'rxjs/operators';
import { AppLocalizationService } from '@app/shared/common/localization/app-localization.service';
import { IContentBaseComponent } from './IContentBaseComponent';
import { DiyFormService } from '../shared/diy-form-service';

@Injectable()
export class ContentFormService {

    constructor(
        private _contentService: ContentServiceProxy,
        public _diyFormService :DiyFormService,
        private _appLocalizationService: AppLocalizationService) {

    }

    getConfigAndPermisions(
        vm: IContentBaseComponent,
        callback: { (result: ConfigAndPermisions): void }
    ) {
        vm.isLoading = true;

        this._contentService.getConfigAndPermisions(vm.kind, vm.customKey)
            .pipe(finalize(() => vm.isLoading = false))
            .subscribe(result => {
            if (result) {
                callback(result);
            }
        });
    }

    beforeSubmit(config: FormField[], field: { [key: string]: any }) {
        this._diyFormService.beforeSubmit(config,field);
    }

    afterLoadData(config: FormField[], data: { [key: string]: any }) {
        this._diyFormService.afterLoadData(config,data);
    }

    // 目前项目中不再使用JQuery
    initCtrls(vm: IContentBaseComponent) {
        
    }

    getDetailPageTemplate(config: ContentConfig) :string {
        return this._diyFormService.getDetailPageTemplate(config.detail);
    }
}
